home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / gcc-2.95.3-3 / info / g77.info-19 < prev    next >
Encoding:
GNU Info File  |  2001-07-15  |  50.1 KB  |  1,368 lines

  1. This is Info file f/g77.info, produced by Makeinfo version 1.68 from
  2. the input file ./f/g77.texi.
  3.  
  4. INFO-DIR-SECTION Programming
  5. START-INFO-DIR-ENTRY
  6. * g77: (g77).                  The GNU Fortran compiler.
  7. END-INFO-DIR-ENTRY
  8.    This file documents the use and the internals of the GNU Fortran
  9. (`g77') compiler.  It corresponds to the GCC-2.95 version of `g77'.
  10.  
  11.    Published by the Free Software Foundation 59 Temple Place - Suite 330
  12. Boston, MA 02111-1307 USA
  13.  
  14.    Copyright (C) 1995-1999 Free Software Foundation, Inc.
  15.  
  16.    Permission is granted to make and distribute verbatim copies of this
  17. manual provided the copyright notice and this permission notice are
  18. preserved on all copies.
  19.  
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the sections entitled "GNU General Public License," "Funding for
  23. Free Software," and "Protect Your Freedom--Fight `Look And Feel'" are
  24. included exactly as in the original, and provided that the entire
  25. resulting derived work is distributed under the terms of a permission
  26. notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that the sections entitled "GNU General Public
  31. License," "Funding for Free Software," and "Protect Your Freedom--Fight
  32. `Look And Feel'", and this permission notice, may be included in
  33. translations approved by the Free Software Foundation instead of in the
  34. original English.
  35.  
  36.    Contributed by James Craig Burley (<craig@jcb-sc.com>).  Inspired by
  37. a first pass at translating `g77-0.5.16/f/DOC' that was contributed to
  38. Craig by David Ronis (<ronis@onsager.chem.mcgill.ca>).
  39.  
  40. 
  41. File: g77.info,  Node: Gotchas (Transforming),  Next: TBD (Transforming),  Prev: ste.c,  Up: Overview of Translation Process
  42.  
  43. Gotchas (Transforming)
  44. ----------------------
  45.  
  46.    This section is not about transforming "gotchas" into something else.
  47. It is about the weirder aspects of transforming Fortran, however that's
  48. defined, into a more modern, canonical form.
  49.  
  50. Multi-character Lexemes
  51. .......................
  52.  
  53.    Each lexeme carries with it a pointer to where it appears in the
  54. source.
  55.  
  56.    To provide the ability for diagnostics to point to column numbers,
  57. in addition to line numbers and names, lexemes that represent more than
  58. one (significant) character in the source code need, generally, to
  59. provide pointers to where each *character* appears in the source.
  60.  
  61.    This provides the ability to properly identify the precise location
  62. of the problem in code like
  63.  
  64.      SUBROUTINE X
  65.      END
  66.      BLOCK DATA X
  67.      END
  68.  
  69.    which, in fixed-form source, would result in single lexemes
  70. consisting of the strings `SUBROUTINEX' and `BLOCKDATAX'.  (The problem
  71. is that `X' is defined twice, so a pointer to the `X' in the second
  72. definition, as well as a follow-up pointer to the corresponding pointer
  73. in the first, would be preferable to pointing to the beginnings of the
  74. statements.)
  75.  
  76.    This need also arises when parsing (and diagnosing) `FORMAT'
  77. statements.
  78.  
  79.    Further, it arises when diagnosing `FMT=' specifiers that contain
  80. constants (or partial constants, or even propagated constants!)  in I/O
  81. statements, as in:
  82.  
  83.      PRINT '(I2, 3HAB)', J
  84.  
  85.    (A pointer to the beginning of the prematurely-terminated Hollerith
  86. constant, and/or to the close parenthese, is preferable to a pointer to
  87. the open-parenthese or the apostrophe that precedes it.)
  88.  
  89.    Multi-character lexemes, which would seem to naturally include at
  90. least digit strings, alphanumeric strings, `CHARACTER' constants, and
  91. Hollerith constants, therefore need to provide location information on
  92. each character.  (Maybe Hollerith constants don't, but it's unnecessary
  93. to except them.)
  94.  
  95.    The question then arises, what about *other* multi-character lexemes,
  96. such as `**' and `//', and Fortran 90's `(/', `/)', `::', and so on?
  97.  
  98.    Turns out there's a need to identify the location of the second
  99. character of these two-character lexemes.  For example, in `I(/J) = K',
  100. the slash needs to be diagnosed as the problem, not the open parenthese.
  101. Similarly, it is preferable to diagnose the second slash in `I = J //
  102. K' rather than the first, given the implicit typing rules, which would
  103. result in the compiler disallowing the attempted concatenation of two
  104. integers.  (Though, since that's more of a semantic issue, it's not
  105. *that* much preferable.)
  106.  
  107.    Even sequences that could be parsed as digit strings could use
  108. location info, for example, to diagnose the `9' in the octal constant
  109. `O'129''.  (This probably will be parsed as a character string, to be
  110. consistent with the parsing of `Z'129A''.)
  111.  
  112.    To avoid the hassle of recording the location of the second
  113. character, while also preserving the general rule that each significant
  114. character is distinctly pointed to by the lexeme that contains it, it's
  115. best to simply not have any fixed-size lexemes larger than one
  116. character.
  117.  
  118.    This new design is expected to make checking for two `*' lexemes in
  119. a row much easier than the old design, so this is not much of a
  120. sacrifice.  It probably makes the lexer much easier to implement than
  121. it makes the parser harder.
  122.  
  123. Space-padding Lexemes
  124. .....................
  125.  
  126.    Certain lexemes need to be padded with virtual spaces when the end
  127. of the line (or file) is encountered.
  128.  
  129.    This is necessary in fixed form, to handle lines that don't extend
  130. to column 72, assuming that's the line length in effect.
  131.  
  132. Bizarre Free-form Hollerith Constants
  133. .....................................
  134.  
  135.    Last I checked, the Fortran 90 standard actually required the
  136. compiler to silently accept something like
  137.  
  138.      FORMAT ( 1 2   Htwelve chars )
  139.  
  140.    as a valid `FORMAT' statement specifying a twelve-character
  141. Hollerith constant.
  142.  
  143.    The implication here is that, since the new lexer is a zero-feedback
  144. one, it won't know that the special case of a `FORMAT' statement being
  145. parsed requires apparently distinct lexemes `1' and `2' to be treated as
  146. a single lexeme.
  147.  
  148.    (This is a horrible misfeature of the Fortran 90 language.  It's one
  149. of many such misfeatures that almost make me want to not support them,
  150. and forge ahead with designing a new "GNU Fortran" language that has
  151. the features, but not the misfeatures, of Fortran 90, and provide
  152. utility programs to do the conversion automatically.)
  153.  
  154.    So, the lexer must gather distinct chunks of decimal strings into a
  155. single lexeme in contexts where a single decimal lexeme might start a
  156. Hollerith constant.
  157.  
  158.    (Which probably means it might as well do that all the time for all
  159. multi-character lexemes, even in free-form mode, leaving it to
  160. subsequent phases to pull them apart as they see fit.)
  161.  
  162.    Compare the treatment of this to how
  163.  
  164.      CHARACTER * 4 5 HEY
  165.  
  166.    and
  167.  
  168.      CHARACTER * 12 HEY
  169.  
  170.    must be treated--the former must be diagnosed, due to the separation
  171. between lexemes, the latter must be accepted as a proper declaration.
  172.  
  173. Hollerith Constants
  174. ...................
  175.  
  176.    Recognizing a Hollerith constant--specifically, that an `H' or `h'
  177. after a digit string begins such a constant--requires some knowledge of
  178. context.
  179.  
  180.    Hollerith constants (such as `2HAB') can appear after:
  181.  
  182.    * `('
  183.  
  184.    * `,'
  185.  
  186.    * `='
  187.  
  188.    * `+', `-', `/'
  189.  
  190.    * `*', except as noted below
  191.  
  192.    Hollerith constants don't appear after:
  193.  
  194.    * `CHARACTER*', which can be treated generally as any `*' that is
  195.      the second lexeme of a statement
  196.  
  197. Confusing Function Keyword
  198. ..........................
  199.  
  200.    While
  201.  
  202.      REAL FUNCTION FOO ()
  203.  
  204.    must be a `FUNCTION' statement and
  205.  
  206.      REAL FUNCTION FOO (5)
  207.  
  208.    must be a type-definition statement,
  209.  
  210.      REAL FUNCTION FOO (NAMES)
  211.  
  212.    where NAMES is a comma-separated list of names, can be one or the
  213. other.
  214.  
  215.    The only way to disambiguate that statement (short of mandating
  216. free-form source or a short maximum length for name for external
  217. procedures) is based on the context of the statement.
  218.  
  219.    In particular, the statement is known to be within an
  220. already-started program unit (but not at the outer level of the
  221. `CONTAINS' block), it is a type-declaration statement.
  222.  
  223.    Otherwise, the statement is a `FUNCTION' statement, in that it
  224. begins a function program unit (external, or, within `CONTAINS',
  225. nested).
  226.  
  227. Weird READ
  228. ..........
  229.  
  230.    The statement
  231.  
  232.      READ (N)
  233.  
  234.    is equivalent to either
  235.  
  236.      READ (UNIT=(N))
  237.  
  238.    or
  239.  
  240.      READ (FMT=(N))
  241.  
  242.    depending on which would be valid in context.
  243.  
  244.    Specifically, if `N' is type `INTEGER', `READ (FMT=(N))' would not
  245. be valid, because parentheses may not be used around `N', whereas they
  246. may around it in `READ (UNIT=(N))'.
  247.  
  248.    Further, if `N' is type `CHARACTER', the opposite is true--`READ
  249. (UNIT=(N))' is not valid, but `READ (FMT=(N))' is.
  250.  
  251.    Strictly speaking, if anything follows
  252.  
  253.      READ (N)
  254.  
  255.    in the statement, whether the first lexeme after the close
  256. parenthese is a comma could be used to disambiguate the two cases,
  257. without looking at the type of `N', because the comma is required for
  258. the `READ (FMT=(N))' interpretation and disallowed for the `READ
  259. (UNIT=(N))' interpretation.
  260.  
  261.    However, in practice, many Fortran compilers allow the comma for the
  262. `READ (UNIT=(N))' interpretation anyway (in that they generally allow a
  263. leading comma before an I/O list in an I/O statement), and much code
  264. takes advantage of this allowance.
  265.  
  266.    (This is quite a reasonable allowance, since the juxtaposition of a
  267. comma-separated list immediately after an I/O control-specification
  268. list, which is also comma-separated, without an intervening comma,
  269. looks sufficiently "wrong" to programmers that they can't resist the
  270. itch to insert the comma.  `READ (I, J), K, L' simply looks cleaner than
  271. `READ (I, J) K, L'.)
  272.  
  273.    So, type-based disambiguation is needed unless strict adherence to
  274. the standard is always assumed, and we're not going to assume that.
  275.  
  276. 
  277. File: g77.info,  Node: TBD (Transforming),  Prev: Gotchas (Transforming),  Up: Overview of Translation Process
  278.  
  279. TBD (Transforming)
  280. ------------------
  281.  
  282.    Continue researching gotchas, designing the transformational process,
  283. and implementing it.
  284.  
  285.    Specific issues to resolve:
  286.  
  287.    * Just where should `INCLUDE' processing take place?
  288.  
  289.      Clearly before (or part of) statement identification (`sta.c'),
  290.      since determining whether `I(J)=K' is a statement-function
  291.      definition or an assignment statement requires knowing the context,
  292.      which in turn requires having processed `INCLUDE' files.
  293.  
  294.    * Just where should (if it was implemented) `USE' processing take
  295.      place?
  296.  
  297.      This gets into the whole issue of how `g77' should handle the
  298.      concept of modules.  I think GNAT already takes on this issue, but
  299.      don't know more than that.  Jim Giles has written extensively on
  300.      `comp.lang.fortran' about his opinions on module handling, as have
  301.      others.  Jim's views should be taken into account.
  302.  
  303.      Actually, Richard M. Stallman (RMS) also has written up some
  304.      guidelines for implementing such things, but I'm not sure where I
  305.      read them.  Perhaps the old <gcc2@cygnus.com> list.
  306.  
  307.      If someone could dig references to these up and get them to me,
  308.      that would be much appreciated!  Even though modules are not on
  309.      the short-term list for implementation, it'd be helpful to know
  310.      *now* how to avoid making them harder to implement them *later*.
  311.  
  312.    * Should the `g77' command become just a script that invokes all the
  313.      various preprocessing that might be needed, thus making it seem
  314.      slower than necessary for legacy code that people are unwilling to
  315.      convert, or should we provide a separate script for that, thus
  316.      encouraging people to convert their code once and for all?
  317.  
  318.      At least, a separate script to behave as old `g77' did, perhaps
  319.      named `g77old', might ease the transition, as might a
  320.      corresponding one that converts source codes named `g77oldnew'.
  321.  
  322.      These scripts would take all the pertinent options `g77' used to
  323.      take and run the appropriate filters, passing the results to `g77'
  324.      or just making new sources out of them (in a subdirectory, leaving
  325.      the user to do the dirty deed of moving or copying them over the
  326.      old sources).
  327.  
  328.    * Do other Fortran compilers provide a prefix syntax to govern the
  329.      treatment of backslashes in `CHARACTER' (or Hollerith) constants?
  330.  
  331.      Knowing what other compilers provide would help.
  332.  
  333.    * Is it okay to drop support for the `-fintrin-case-initcap',
  334.      `-fmatch-case-initcap', `-fsymbol-case-initcap', and
  335.      `-fcase-initcap' options?
  336.  
  337.      I've asked <info-gnu-fortran@gnu.org> for input on this.  Not
  338.      having to support these makes it easier to write the new front end,
  339.      and might also avoid complicated its design.
  340.  
  341. 
  342. File: g77.info,  Node: Philosophy of Code Generation,  Next: Two-pass Design,  Prev: Overview of Translation Process,  Up: Front End
  343.  
  344. Philosophy of Code Generation
  345. =============================
  346.  
  347.    Don't poke the bear.
  348.  
  349.    The `g77' front end generates code via the `gcc' back end.
  350.  
  351.    The `gcc' back end (GBE) is a large, complex labyrinth of intricate
  352. code written in a combination of the C language and specialized
  353. languages internal to `gcc'.
  354.  
  355.    While the *code* that implements the GBE is written in a combination
  356. of languages, the GBE itself is, to the front end for a language like
  357. Fortran, best viewed as a *compiler* that compiles its own, unique,
  358. language.
  359.  
  360.    The GBE's "source", then, is written in this language, which
  361. consists primarily of a combination of calls to GBE functions and
  362. "tree" nodes (which are, themselves, created by calling GBE functions).
  363.  
  364.    So, the `g77' generates code by, in effect, translating the Fortran
  365. code it reads into a form "written" in the "language" of the `gcc' back
  366. end.
  367.  
  368.    This language will heretofore be referred to as "GBEL", for GNU Back
  369. End Language.
  370.  
  371.    GBEL is an evolving language, not fully specified in any published
  372. form as of this writing.  It offers many facilities, but its "core"
  373. facilities are those that corresponding most directly to those needed
  374. to support `gcc' (compiling code written in GNU C).
  375.  
  376.    The `g77' Fortran Front End (FFE) is designed and implemented to
  377. navigate the currents and eddies of ongoing GBEL and `gcc' development
  378. while also delivering on the potential of an integrated FFE (as
  379. compared to using a converter like `f2c' and feeding the output into
  380. `gcc').
  381.  
  382.    Goals of the FFE's code-generation strategy include:
  383.  
  384.    * High likelihood of generation of correct code, or, failing that,
  385.      producing a fatal diagnostic or crashing.
  386.  
  387.    * Generation of highly optimized code, as directed by the user via
  388.      GBE-specific (versus `g77'-specific) constructs, such as
  389.      command-line options.
  390.  
  391.    * Fast overall (FFE plus GBE) compilation.
  392.  
  393.    * Preservation of source-level debugging information.
  394.  
  395.    The strategies historically, and currently, used by the FFE to
  396. achieve these goals include:
  397.  
  398.    * Use of GBEL constructs that most faithfully encapsulate the
  399.      semantics of Fortran.
  400.  
  401.    * Avoidance of GBEL constructs that are so rarely used, or limited
  402.      to use in specialized situations not related to Fortran, that
  403.      their reliability and performance has not yet been established as
  404.      sufficient for use by the FFE.
  405.  
  406.    * Flexible design, to readily accommodate changes to specific
  407.      code-generation strategies, perhaps governed by command-line
  408.      options.
  409.  
  410.    "Don't poke the bear" somewhat summarizes the above strategies.  The
  411. GBE is the bear.  The FFE is designed and implemented to avoid poking it
  412. in ways that are likely to just annoy it.  The FFE usually either
  413. tackles it head-on, or avoids treating it in ways dissimilar to how the
  414. `gcc' front end treats it.
  415.  
  416.    For example, the FFE uses the native array facility in the back end
  417. instead of the lower-level pointer-arithmetic facility used by `gcc'
  418. when compiling `f2c' output).  Theoretically, this presents more
  419. opportunities for optimization, faster compile times, and the
  420. production of more faithful debugging information.  These benefits were
  421. not, however, immediately realized, mainly because `gcc' itself makes
  422. little or no use of the native array facility.
  423.  
  424.    Complex arithmetic is a case study of the evolution of this strategy.
  425. When originally implemented, the GBEL had just evolved its own native
  426. complex-arithmetic facility, so the FFE took advantage of that.
  427.  
  428.    When porting `g77' to 64-bit systems, it was discovered that the GBE
  429. didn't really implement its native complex-arithmetic facility properly.
  430.  
  431.    The short-term solution was to rewrite the FFE to instead use the
  432. lower-level facilities that'd be used by `gcc'-compiled code (assuming
  433. that code, itself, didn't use the native complex type provided, as an
  434. extension, by `gcc'), since these were known to work, and, in any case,
  435. if shown to not work, would likely be rapidly fixed (since they'd
  436. likely not work for vanilla C code in similar circumstances).
  437.  
  438.    However, the rewrite accommodated the original, native approach as
  439. well by offering a command-line option to select it over the emulated
  440. approach.  This allowed users, and especially GBE maintainers, to try
  441. out fixes to complex-arithmetic support in the GBE while `g77'
  442. continued to default to compiling more code correctly, albeit producing
  443. (typically) slower executables.
  444.  
  445.    As of April 1999, it appeared that the last few bugs in the GBE's
  446. support of its native complex-arithmetic facility were worked out.  The
  447. FFE was changed back to default to using that native facility, leaving
  448. emulation as an option.
  449.  
  450.    Other Fortran constructs--arrays, character strings, complex
  451. division, `COMMON' and `EQUIVALENCE' aggregates, and so on--involve
  452. issues similar to those pertaining to complex arithmetic.
  453.  
  454.    So, it is possible that the history of how the FFE handled complex
  455. arithmetic will be repeated, probably in modified form (and hopefully
  456. over shorter timeframes), for some of these other facilities.
  457.  
  458. 
  459. File: g77.info,  Node: Two-pass Design,  Next: Challenges Posed,  Prev: Philosophy of Code Generation,  Up: Front End
  460.  
  461. Two-pass Design
  462. ===============
  463.  
  464.    The FFE does not tell the GBE anything about a program unit until
  465. after the last statement in that unit has been parsed.  (A program unit
  466. is a Fortran concept that corresponds, in the C world, mostly closely
  467. to functions definitions in ISO C.  That is, a program unit in Fortran
  468. is like a top-level function in C.  Nested functions, found among the
  469. extensions offered by GNU C, correspond roughly to Fortran's statement
  470. functions.)
  471.  
  472.    So, while parsing the code in a program unit, the FFE saves up all
  473. the information on statements, expressions, names, and so on, until it
  474. has seen the last statement.
  475.  
  476.    At that point, the FFE revisits the saved information (in what
  477. amounts to a second "pass" over the program unit) to perform the actual
  478. translation of the program unit into GBEL, ultimating in the generation
  479. of assembly code for it.
  480.  
  481.    Some lookahead is performed during this second pass, so the FFE
  482. could be viewed as a "two-plus-pass" design.
  483.  
  484. * Menu:
  485.  
  486. * Two-pass Code::
  487. * Why Two Passes::
  488.  
  489. 
  490. File: g77.info,  Node: Two-pass Code,  Next: Why Two Passes,  Up: Two-pass Design
  491.  
  492. Two-pass Code
  493. -------------
  494.  
  495.    Most of the code that turns the first pass (parsing) into a second
  496. pass for code generation is in `egcs/gcc/f/std.c'.
  497.  
  498.    It has external functions, called mainly by siblings in
  499. `egcs/gcc/f/stc.c', that record the information on statements and
  500. expressions in the order they are seen in the source code.  These
  501. functions save that information.
  502.  
  503.    It also has an external function that revisits that information,
  504. calling the siblings in `egcs/gcc/f/ste.c', which handles the actual
  505. code generation (by generating GBEL code, that is, by calling GBE
  506. routines to represent and specify expressions, statements, and so on).
  507.  
  508. 
  509. File: g77.info,  Node: Why Two Passes,  Prev: Two-pass Code,  Up: Two-pass Design
  510.  
  511. Why Two Passes
  512. --------------
  513.  
  514.    The need for two passes was not immediately evident during the
  515. design and implementation of the code in the FFE that was to produce
  516. GBEL.  Only after a few kludges, to handle things like
  517. incorrectly-guessed `ASSIGN' label nature, had been implemented, did
  518. enough evidence pile up to make it clear that `std.c' had to be
  519. introduced to intercept, save, then revisit as part of a second pass,
  520. the digested contents of a program unit.
  521.  
  522.    Other such missteps have occurred during the evolution of the FFE,
  523. because of the different goals of the FFE and the GBE.
  524.  
  525.    Because the GBE's original, and still primary, goal was to directly
  526. support the GNU C language, the GBEL, and the GBE itself, requires more
  527. complexity on the part of most front ends than it requires of `gcc''s.
  528.  
  529.    For example, the GBEL offers an interface that permits the `gcc'
  530. front end to implement most, or all, of the language features it
  531. supports, without the front end having to make use of non-user-defined
  532. variables.  (It's almost certainly the case that all of K&R C, and
  533. probably ANSI C as well, is handled by the `gcc' front end without
  534. declaring such variables.)
  535.  
  536.    The FFE, on the other hand, must resort to a variety of "tricks" to
  537. achieve its goals.
  538.  
  539.    Consider the following C code:
  540.  
  541.      int
  542.      foo (int a, int b)
  543.      {
  544.        int c = 0;
  545.      
  546.        if ((c = bar (c)) == 0)
  547.          goto done;
  548.      
  549.        quux (c << 1);
  550.      
  551.      done:
  552.        return c;
  553.      }
  554.  
  555.    Note what kinds of objects are declared, or defined, before their
  556. use, and before any actual code generation involving them would
  557. normally take place:
  558.  
  559.    * Return type of function
  560.  
  561.    * Entry point(s) of function
  562.  
  563.    * Dummy arguments
  564.  
  565.    * Variables
  566.  
  567.    * Initial values for variables
  568.  
  569.    Whereas, the following items can, and do, suddenly appear "out of
  570. the blue" in C:
  571.  
  572.    * Label references
  573.  
  574.    * Function references
  575.  
  576.    Not surprisingly, the GBE faithfully permits the latter set of items
  577. to be "discovered" partway through GBEL "programs", just as they are
  578. permitted to in C.
  579.  
  580.    Yet, the GBE has tended, at least in the past, to be reticent to
  581. fully support similar "late" discovery of items in the former set.
  582.  
  583.    This makes Fortran a poor fit for the "safe" subset of GBEL.
  584. Consider:
  585.  
  586.            FUNCTION X (A, ARRAY, ID1)
  587.            CHARACTER*(*) A
  588.            DOUBLE PRECISION X, Y, Z, TMP, EE, PI
  589.            REAL ARRAY(ID1*ID2)
  590.            COMMON ID2
  591.            EXTERNAL FRED
  592.      
  593.            ASSIGN 100 TO J
  594.            CALL FOO (I)
  595.            IF (I .EQ. 0) PRINT *, A(0)
  596.            GOTO 200
  597.      
  598.            ENTRY Y (Z)
  599.            ASSIGN 101 TO J
  600.      200   PRINT *, A(1)
  601.            READ *, TMP
  602.            GOTO J
  603.      100   X = TMP * EE
  604.            RETURN
  605.      101   Y = TMP * PI
  606.            CALL FRED
  607.            DATA EE, PI /2.71D0, 3.14D0/
  608.            END
  609.  
  610.    Here are some observations about the above code, which, while
  611. somewhat contrived, conforms to the FORTRAN 77 and Fortran 90 standards:
  612.  
  613.    * The return type of function `X' is not known until the `DOUBLE
  614.      PRECISION' line has been parsed.
  615.  
  616.    * Whether `A' is a function or a variable is not known until the
  617.      `PRINT *, A(0)' statement has been parsed.
  618.  
  619.    * The bounds of the array of argument `ARRAY' depend on a
  620.      computation involving the subsequent argument `ID1' and the
  621.      blank-common member `ID2'.
  622.  
  623.    * Whether `Y' and `Z' are local variables, additional function entry
  624.      points, or dummy arguments to additional entry points is not known
  625.      until the `ENTRY' statement is parsed.
  626.  
  627.    * Similarly, whether `TMP' is a local variable is not known until
  628.      the `READ *, TMP' statement is parsed.
  629.  
  630.    * The initial values for `EE' and `PI' are not known until after the
  631.      `DATA' statement is parsed.
  632.  
  633.    * Whether `FRED' is a function returning type `REAL' or a subroutine
  634.      (which can be thought of as returning type `void' *or*, to support
  635.      alternate returns in a simple way, type `int') is not known until
  636.      the `CALL FRED' statement is parsed.
  637.  
  638.    * Whether `100' is a `FORMAT' label or the label of an executable
  639.      statement is not known until the `X =' statement is parsed.
  640.      (These two types of labels get *very* different treatment,
  641.      especially when `ASSIGN''ed.)
  642.  
  643.    * That `J' is a local variable is not known until the first `ASSIGN'
  644.      statement is parsed.  (This happens *after* executable code has
  645.      been seen.)
  646.  
  647.    Very few of these "discoveries" can be accommodated by the GBE as it
  648. has evolved over the years.  The GBEL doesn't support several of them,
  649. and those it might appear to support don't always work properly,
  650. especially in combination with other GBEL and GBE features, as
  651. implemented in the GBE.
  652.  
  653.    (Had the GBE and its GBEL originally evolved to support `g77', the
  654. shoe would be on the other foot, so to speak--most, if not all, of the
  655. above would be directly supported by the GBEL, and a few C constructs
  656. would probably not, as they are in reality, be supported.  Both this
  657. mythical, and today's real, GBE caters to its GBEL by, sometimes,
  658. scrambling around, cleaning up after itself--after discovering that
  659. assumptions it made earlier during code generation are incorrect.)
  660.  
  661.    So, the FFE handles these discrepancies--between the order in which
  662. it discovers facts about the code it is compiling, and the order in
  663. which the GBEL and GBE support such discoveries--by performing what
  664. amounts to two passes over each program unit.
  665.  
  666.    (A few ambiguities can remain at that point, such as whether, given
  667. `EXTERNAL BAZ' and no other reference to `BAZ' in the program unit, it
  668. is a subroutine, a function, or a block-data--which, in C-speak,
  669. governs its declared return type.  Fortunately, these distinctions are
  670. easily finessed for the procedure, library, and object-file interfaces
  671. supported by `g77'.)
  672.  
  673. 
  674. File: g77.info,  Node: Challenges Posed,  Next: Transforming Statements,  Prev: Two-pass Design,  Up: Front End
  675.  
  676. Challenges Posed
  677. ================
  678.  
  679.    Consider the following Fortran code, which uses various extensions
  680. (including some to Fortran 90):
  681.  
  682.      SUBROUTINE X(A)
  683.      CHARACTER*(*) A
  684.      COMPLEX CFUNC
  685.      INTEGER*2 CLOCKS(200)
  686.      INTEGER IFUNC
  687.      
  688.      CALL SYSTEM_CLOCK (CLOCKS (IFUNC (CFUNC ('('//A//')'))))
  689.  
  690.    The above poses the following challenges to any Fortran compiler
  691. that uses run-time interfaces, and a run-time library, roughly similar
  692. to those used by `g77':
  693.  
  694.    * Assuming the library routine that supports `SYSTEM_CLOCK' expects
  695.      to set an `INTEGER*4' variable via its `COUNT' argument, the
  696.      compiler must make available to it a temporary variable of that
  697.      type.
  698.  
  699.    * Further, after the `SYSTEM_CLOCK' library routine returns, the
  700.      compiler must ensure that the temporary variable it wrote is
  701.      copied into the appropriate element of the `CLOCKS' array.  (This
  702.      assumes the compiler doesn't just reject the code, which it should
  703.      if it is compiling under some kind of a "strict" option.)
  704.  
  705.    * To determine the correct index into the `CLOCKS' array, (putting
  706.      aside the fact that the index, in this particular case, need not
  707.      be computed until after the `SYSTEM_CLOCK' library routine
  708.      returns), the compiler must ensure that the `IFUNC' function is
  709.      called.
  710.  
  711.      That requires evaluating its argument, which requires, for `g77'
  712.      (assuming `-ff2c' is in force), reserving a temporary variable of
  713.      type `COMPLEX' for use as a repository for the return value being
  714.      computed by `CFUNC'.
  715.  
  716.    * Before invoking `CFUNC', is argument must be evaluated, which
  717.      requires allocating, at run time, a temporary large enough to hold
  718.      the result of the concatenation, as well as actually performing
  719.      the concatenation.
  720.  
  721.    * The large temporary needed during invocation of `CFUNC' should,
  722.      ideally, be deallocated (or, at least, left to the GBE to dispose
  723.      of, as it sees fit) as soon as `CFUNC' returns, which means before
  724.      `IFUNC' is called (as it might need a lot of dynamically allocated
  725.      memory).
  726.  
  727.    `g77' currently doesn't support all of the above, but, so that it
  728. might someday, it has evolved to handle at least some of the above
  729. requirements.
  730.  
  731.    Meeting the above requirements is made more challenging by
  732. conforming to the requirements of the GBEL/GBE combination.
  733.  
  734. 
  735. File: g77.info,  Node: Transforming Statements,  Next: Transforming Expressions,  Prev: Challenges Posed,  Up: Front End
  736.  
  737. Transforming Statements
  738. =======================
  739.  
  740.    Most Fortran statements are given their own block, and, for
  741. temporary variables they might need, their own scope.  (A block is what
  742. distinguishes `{ foo (); }' from just `foo ();' in C.  A scope is
  743. included with every such block, providing a distinct name space for
  744. local variables.)
  745.  
  746.    Label definitions for the statement precede this block, so `10 PRINT
  747. *, I' is handled more like `fl10: { ... }' than `{ fl10: ... }' (where
  748. `fl10' is just a notation meaning "Fortran Label 10" for the purposes
  749. of this document).
  750.  
  751. * Menu:
  752.  
  753. * Statements Needing Temporaries::
  754. * Transforming DO WHILE::
  755. * Transforming Iterative DO::
  756. * Transforming Block IF::
  757. * Transforming SELECT CASE::
  758.  
  759. 
  760. File: g77.info,  Node: Statements Needing Temporaries,  Next: Transforming DO WHILE,  Up: Transforming Statements
  761.  
  762. Statements Needing Temporaries
  763. ------------------------------
  764.  
  765.    Any temporaries needed during, but not beyond, execution of a
  766. Fortran statement, are made local to the scope of that statement's
  767. block.
  768.  
  769.    This allows the GBE to share storage for these temporaries among the
  770. various statements without the FFE having to manage that itself.
  771.  
  772.    (The GBE could, of course, decide to optimize management of these
  773. temporaries.  For example, it could, theoretically, schedule some of
  774. the computations involving these temporaries to occur in parallel.
  775. More practically, it might leave the storage for some temporaries
  776. "live" beyond their scopes, to reduce the number of manipulations of
  777. the stack pointer at run time.)
  778.  
  779.    Temporaries needed across distinct statement boundaries usually are
  780. associated with Fortran blocks (such as `DO'/`END DO').  (Also, there
  781. might be temporaries not associated with blocks at all--these would be
  782. in the scope of the entire program unit.)
  783.  
  784.    Each Fortran block *should* get its own block/scope in the GBE.
  785. This is best, because it allows temporaries to be more naturally
  786. handled.  However, it might pose problems when handling labels (in
  787. particular, when they're the targets of `GOTO's outside the Fortran
  788. block), and generally just hassling with replicating parts of the `gcc'
  789. front end (because the FFE needs to support an arbitrary number of
  790. nested back-end blocks if each Fortran block gets one).
  791.  
  792.    So, there might still be a need for top-level temporaries, whose
  793. "owning" scope is that of the containing procedure.
  794.  
  795.    Also, there seems to be problems declaring new variables after
  796. generating code (within a block) in the back end, leading to, e.g.,
  797. `label not defined before binding contour' or similar messages, when
  798. compiling with `-fstack-check' or when compiling for certain targets.
  799.  
  800.    Because of that, and because sometimes these temporaries are not
  801. discovered until in the middle of of generating code for an expression
  802. statement (as in the case of the optimization for `X**I'), it seems
  803. best to always pre-scan all the expressions that'll be expanded for a
  804. block before generating any of the code for that block.
  805.  
  806.    This pre-scan then handles discovering and declaring, to the back
  807. end, the temporaries needed for that block.
  808.  
  809.    It's also important to treat distinct items in an I/O list as
  810. distinct statements deserving their own blocks.  That's because there's
  811. a requirement that each I/O item be fully processed before the next one,
  812. which matters in cases like `READ (*,*), I, A(I)'--the element of `A'
  813. read in the second item *must* be determined from the value of `I' read
  814. in the first item.
  815.  
  816. 
  817. File: g77.info,  Node: Transforming DO WHILE,  Next: Transforming Iterative DO,  Prev: Statements Needing Temporaries,  Up: Transforming Statements
  818.  
  819. Transforming DO WHILE
  820. ---------------------
  821.  
  822.    `DO WHILE(expr)' *must* be implemented so that temporaries needed to
  823. evaluate `expr' are generated just for the test, each time.
  824.  
  825.    Consider how `DO WHILE (A//B .NE. 'END'); ...; END DO' is
  826. transformed:
  827.  
  828.      for (;;)
  829.        {
  830.          int temp0;
  831.      
  832.          {
  833.            char temp1[large];
  834.      
  835.            libg77_catenate (temp1, a, b);
  836.            temp0 = libg77_ne (temp1, 'END');
  837.          }
  838.      
  839.          if (! temp0)
  840.            break;
  841.      
  842.          ...
  843.        }
  844.  
  845.    In this case, it seems like a time/space tradeoff between allocating
  846. and deallocating `temp1' for each iteration and allocating it just once
  847. for the entire loop.
  848.  
  849.    However, if `temp1' is allocated just once for the entire loop, it
  850. could be the wrong size for subsequent iterations of that loop in cases
  851. like `DO WHILE (A(I:J)//B .NE. 'END')', because the body of the loop
  852. might modify `I' or `J'.
  853.  
  854.    So, the above implementation is used, though a more optimal one can
  855. be used in specific circumstances.
  856.  
  857. 
  858. File: g77.info,  Node: Transforming Iterative DO,  Next: Transforming Block IF,  Prev: Transforming DO WHILE,  Up: Transforming Statements
  859.  
  860. Transforming Iterative DO
  861. -------------------------
  862.  
  863.    An iterative `DO' loop (one that specifies an iteration variable) is
  864. required by the Fortran standards to be implemented as though an
  865. iteration count is computed before entering the loop body, and that
  866. iteration count used to determine the number of times the loop body is
  867. to be performed (assuming the loop isn't cut short via `GOTO' or
  868. `EXIT').
  869.  
  870.    The FFE handles this by allocating a temporary variable to contain
  871. the computed number of iterations.  Since this variable must be in a
  872. scope that includes the entire loop, a GBEL block is created for that
  873. loop, and the variable declared as belonging to the scope of that block.
  874.  
  875. 
  876. File: g77.info,  Node: Transforming Block IF,  Next: Transforming SELECT CASE,  Prev: Transforming Iterative DO,  Up: Transforming Statements
  877.  
  878. Transforming Block IF
  879. ---------------------
  880.  
  881.    Consider:
  882.  
  883.      SUBROUTINE X(A,B,C)
  884.      CHARACTER*(*) A, B, C
  885.      LOGICAL LFUNC
  886.      
  887.      IF (LFUNC (A//B)) THEN
  888.        CALL SUBR1
  889.      ELSE IF (LFUNC (A//C)) THEN
  890.        CALL SUBR2
  891.      ELSE
  892.        CALL SUBR3
  893.      END
  894.  
  895.    The arguments to the two calls to `LFUNC' require dynamic allocation
  896. (at run time), but are not required during execution of the `CALL'
  897. statements.
  898.  
  899.    So, the scopes of those temporaries must be within blocks inside the
  900. block corresponding to the Fortran `IF' block.
  901.  
  902.    This cannot be represented "naturally" in vanilla C, nor in GBEL.
  903. The `if', `elseif', `else', and `endif' constructs provided by both
  904. languages must, for a given `if' block, share the same C/GBE block.
  905.  
  906.    Therefore, any temporaries needed during evaluation of `expr' while
  907. executing `ELSE IF(expr)' must either have been predeclared at the top
  908. of the corresponding `IF' block, or declared within a new block for
  909. that `ELSE IF'--a block that, since it cannot contain the `else' or
  910. `else if' itself (due to the above requirement), actually implements
  911. the rest of the `IF' block's `ELSE IF' and `ELSE' statements within an
  912. inner block.
  913.  
  914.    The FFE takes the latter approach.
  915.  
  916. 
  917. File: g77.info,  Node: Transforming SELECT CASE,  Prev: Transforming Block IF,  Up: Transforming Statements
  918.  
  919. Transforming SELECT CASE
  920. ------------------------
  921.  
  922.    `SELECT CASE' poses a few interesting problems for code generation,
  923. if efficiency and frugal stack management are important.
  924.  
  925.    Consider `SELECT CASE (I('PREFIX'//A))', where `A' is
  926. `CHARACTER*(*)'.  In a case like this--basically, in any case where
  927. largish temporaries are needed to evaluate the expression--those
  928. temporaries should not be "live" during execution of any of the `CASE'
  929. blocks.
  930.  
  931.    So, evaluation of the expression is best done within its own block,
  932. which in turn is within the `SELECT CASE' block itself (which contains
  933. the code for the CASE blocks as well, though each within their own
  934. block).
  935.  
  936.    Otherwise, we'd have the rough equivalent of this pseudo-code:
  937.  
  938.      {
  939.        char temp[large];
  940.      
  941.        libg77_catenate (temp, 'prefix', a);
  942.      
  943.        switch (i (temp))
  944.          {
  945.          case 0:
  946.            ...
  947.          }
  948.      }
  949.  
  950.    And that would leave temp[large] in scope during the CASE blocks
  951. (although a clever back end *could* see that it isn't referenced in
  952. them, and thus free that temp before executing the blocks).
  953.  
  954.    So this approach is used instead:
  955.  
  956.      {
  957.        int temp0;
  958.      
  959.        {
  960.          char temp1[large];
  961.      
  962.          libg77_catenate (temp1, 'prefix', a);
  963.          temp0 = i (temp1);
  964.        }
  965.      
  966.        switch (temp0)
  967.          {
  968.          case 0:
  969.            ...
  970.          }
  971.      }
  972.  
  973.    Note how `temp1' goes out of scope before starting the switch, thus
  974. making it easy for a back end to free it.
  975.  
  976.    The problem *that* solution has, however, is with `SELECT
  977. CASE('prefix'//A)' (which is currently not supported).
  978.  
  979.    Unless the GBEL is extended to support arbitrarily long character
  980. strings in its `case' facility, the FFE has to implement `SELECT CASE'
  981. on `CHARACTER' (probably excepting `CHARACTER*1') using a cascade of
  982. `if', `elseif', `else', and `endif' constructs in GBEL.
  983.  
  984.    To prevent the (potentially large) temporary, needed to hold the
  985. selected expression itself (`'prefix'//A'), from being in scope during
  986. execution of the `CASE' blocks, two approaches are available:
  987.  
  988.    * Pre-evaluate all the `CASE' tests, producing an integer ordinal
  989.      that is used, a la `temp0' in the earlier example, as if `SELECT
  990.      CASE(temp0)' had been written.
  991.  
  992.      Each corresponding `CASE' is replaced with `CASE(I)', where I is
  993.      the ordinal for that case, determined while, or before, generating
  994.      the cascade of `if'-related constructs to cope with `CHARACTER'
  995.      selection.
  996.  
  997.    * Make `temp0' above just large enough to hold the longest `CASE'
  998.      string that'll actually be compared against the expression (in
  999.      this case, `'prefix'//A').
  1000.  
  1001.      Since that length must be constant (because `CASE' expressions are
  1002.      all constant), it won't be so large, and, further, `temp1' need
  1003.      not be dynamically allocated, since normal `CHARACTER' assignment
  1004.      can be used into the fixed-length `temp0'.
  1005.  
  1006.    Both of these solutions require `SELECT CASE' implementation to be
  1007. changed so all the corresponding `CASE' statements are seen during the
  1008. actual code generation for `SELECT CASE'.
  1009.  
  1010. 
  1011. File: g77.info,  Node: Transforming Expressions,  Next: Internal Naming Conventions,  Prev: Transforming Statements,  Up: Front End
  1012.  
  1013. Transforming Expressions
  1014. ========================
  1015.  
  1016.    The interactions between statements, expressions, and subexpressions
  1017. at program run time can be viewed as:
  1018.  
  1019.      ACTION(EXPR)
  1020.  
  1021.    Here, ACTION is the series of steps performed to effect the
  1022. statement, and EXPR is the expression whose value is used by ACTION.
  1023.  
  1024.    Expanding the above shows a typical order of events at run time:
  1025.  
  1026.      Evaluate EXPR
  1027.      Perform ACTION, using result of evaluation of EXPR
  1028.      Clean up after evaluating EXPR
  1029.  
  1030.    So, if evaluating EXPR requires allocating memory, that memory can
  1031. be freed before performing ACTION only if it is not needed to hold the
  1032. result of evaluating EXPR.  Otherwise, it must be freed no sooner than
  1033. after ACTION has been performed.
  1034.  
  1035.    The above are recursive definitions, in the sense that they apply to
  1036. subexpressions of EXPR.
  1037.  
  1038.    That is, evaluating EXPR involves evaluating all of its
  1039. subexpressions, performing the ACTION that computes the result value of
  1040. EXPR, then cleaning up after evaluating those subexpressions.
  1041.  
  1042.    The recursive nature of this evaluation is implemented via
  1043. recursive-descent transformation of the top-level statements, their
  1044. expressions, *their* subexpressions, and so on.
  1045.  
  1046.    However, that recursive-descent transformation is, due to the nature
  1047. of the GBEL, focused primarily on generating a *single* stream of code
  1048. to be executed at run time.
  1049.  
  1050.    Yet, from the above, it's clear that multiple streams of code must
  1051. effectively be simultaneously generated during the recursive-descent
  1052. analysis of statements.
  1053.  
  1054.    The primary stream implements the primary ACTION items, while at
  1055. least two other streams implement the evaluation and clean-up items.
  1056.  
  1057.    Requirements imposed by expressions include:
  1058.  
  1059.    * Whether the caller needs to have a temporary ready to hold the
  1060.      value of the expression.
  1061.  
  1062.    * Other stuff???
  1063.  
  1064. 
  1065. File: g77.info,  Node: Internal Naming Conventions,  Prev: Transforming Expressions,  Up: Front End
  1066.  
  1067. Internal Naming Conventions
  1068. ===========================
  1069.  
  1070.    Names exported by FFE modules have the following
  1071. (regular-expression) forms.  Note that all names beginning `ffeMOD' or
  1072. `FFEMOD', where MOD is lowercase or uppercase alphanumerics,
  1073. respectively, are exported by the module `ffeMOD', with the source code
  1074. doing the exporting in `MOD.h'.  (Usually, the source code for the
  1075. implementation is in `MOD.c'.)
  1076.  
  1077.    Identifiers that don't fit the following forms are not considered
  1078. exported, even if they are according to the C language.  (For example,
  1079. they might be made available to other modules solely for use within
  1080. expansions of exported macros, not for use within any source code in
  1081. those other modules.)
  1082.  
  1083. `ffeMOD'
  1084.      The single typedef exported by the module.
  1085.  
  1086. `FFEUMOD_[A-Z][A-Z0-9_]*'
  1087.      (Where UMOD is the uppercase for of MOD.)
  1088.  
  1089.      A `#define' or `enum' constant of the type `ffeMOD'.
  1090.  
  1091. `ffeMOD[A-Z][A-Z][a-z0-9]*'
  1092.      A typedef exported by the module.
  1093.  
  1094.      The portion of the identifier after `ffeMOD' is referred to as
  1095.      `ctype', a capitalized (mixed-case) form of `type'.
  1096.  
  1097. `FFEUMOD_TYPE[A-Z][A-Z0-9_]*[A-Z0-9]?'
  1098.      (Where UMOD is the uppercase for of MOD.)
  1099.  
  1100.      A `#define' or `enum' constant of the type `ffeMODTYPE', where
  1101.      TYPE is the lowercase form of CTYPE in an exported typedef.
  1102.  
  1103. `ffeMOD_VALUE'
  1104.      A function that does or returns something, as described by VALUE
  1105.      (see below).
  1106.  
  1107. `ffeMOD_VALUE_INPUT'
  1108.      A function that does or returns something based primarily on the
  1109.      thing described by INPUT (see below).
  1110.  
  1111.    Below are names used for VALUE and INPUT, along with their
  1112. definitions.
  1113.  
  1114. `col'
  1115.      A column number within a line (first column is number 1).
  1116.  
  1117. `file'
  1118.      An encapsulation of a file's name.
  1119.  
  1120. `find'
  1121.      Looks up an instance of some type that matches specified criteria,
  1122.      and returns that, even if it has to create a new instance or crash
  1123.      trying to find it (as appropriate).
  1124.  
  1125. `initialize'
  1126.      Initializes, usually a module.  No type.
  1127.  
  1128. `int'
  1129.      A generic integer of type `int'.
  1130.  
  1131. `is'
  1132.      A generic integer that contains a true (non-zero) or false (zero)
  1133.      value.
  1134.  
  1135. `len'
  1136.      A generic integer that contains the length of something.
  1137.  
  1138. `line'
  1139.      A line number within a source file, or a global line number.
  1140.  
  1141. `lookup'
  1142.      Looks up an instance of some type that matches specified criteria,
  1143.      and returns that, or returns nil.
  1144.  
  1145. `name'
  1146.      A `text' that points to a name of something.
  1147.  
  1148. `new'
  1149.      Makes a new instance of the indicated type.  Might return an
  1150.      existing one if appropriate--if so, similar to `find' without
  1151.      crashing.
  1152.  
  1153. `pt'
  1154.      Pointer to a particular character (line, column pairs) in the
  1155.      input file (source code being compiled).
  1156.  
  1157. `run'
  1158.      Performs some herculean task.  No type.
  1159.  
  1160. `terminate'
  1161.      Terminates, usually a module.  No type.
  1162.  
  1163. `text'
  1164.      A `char *' that points to generic text.
  1165.  
  1166. 
  1167. File: g77.info,  Node: Diagnostics,  Next: Index,  Prev: Front End,  Up: Top
  1168.  
  1169. Diagnostics
  1170. ***********
  1171.  
  1172.    Some diagnostics produced by `g77' require sufficient explanation
  1173. that the explanations are given below, and the diagnostics themselves
  1174. identify the appropriate explanation.
  1175.  
  1176.    Identification uses the GNU Info format--specifically, the `info'
  1177. command that displays the explanation is given within square brackets
  1178. in the diagnostic.  For example:
  1179.  
  1180.      foo.f:5: Invalid statement [info -f g77 M FOOEY]
  1181.  
  1182.    More details about the above diagnostic is found in the `g77' Info
  1183. documentation, menu item `M', submenu item `FOOEY', which is displayed
  1184. by typing the UNIX command `info -f g77 M FOOEY'.
  1185.  
  1186.    Other Info readers, such as EMACS, may be just as easily used to
  1187. display the pertinent node.  In the above example, `g77' is the Info
  1188. document name, `M' is the top-level menu item to select, and, in that
  1189. node (named `Diagnostics', the name of this chapter, which is the very
  1190. text you're reading now), `FOOEY' is the menu item to select.
  1191.  
  1192. * Menu:
  1193.  
  1194. * CMPAMBIG::    Ambiguous use of intrinsic.
  1195. * EXPIMP::      Intrinsic used explicitly and implicitly.
  1196. * INTGLOB::     Intrinsic also used as name of global.
  1197. * LEX::         Various lexer messages
  1198. * GLOBALS::     Disagreements about globals.
  1199. * LINKFAIL::    When linking `f771' fails.
  1200. * Y2KBAD::      Use of non-Y2K-compliant intrinsic.
  1201.  
  1202. 
  1203. File: g77.info,  Node: CMPAMBIG,  Next: EXPIMP,  Up: Diagnostics
  1204.  
  1205. `CMPAMBIG'
  1206. ==========
  1207.  
  1208.      Ambiguous use of intrinsic INTRINSIC ...
  1209.  
  1210.    The type of the argument to the invocation of the INTRINSIC
  1211. intrinsic is a `COMPLEX' type other than `COMPLEX(KIND=1)'.  Typically,
  1212. it is `COMPLEX(KIND=2)', also known as `DOUBLE COMPLEX'.
  1213.  
  1214.    The interpretation of this invocation depends on the particular
  1215. dialect of Fortran for which the code was written.  Some dialects
  1216. convert the real part of the argument to `REAL(KIND=1)', thus losing
  1217. precision; other dialects, and Fortran 90, do no such conversion.
  1218.  
  1219.    So, GNU Fortran rejects such invocations except under certain
  1220. circumstances, to avoid making an incorrect assumption that results in
  1221. generating the wrong code.
  1222.  
  1223.    To determine the dialect of the program unit, perhaps even whether
  1224. that particular invocation is properly coded, determine how the result
  1225. of the intrinsic is used.
  1226.  
  1227.    The result of INTRINSIC is expected (by the original programmer) to
  1228. be `REAL(KIND=1)' (the non-Fortran-90 interpretation) if:
  1229.  
  1230.    * It is passed as an argument to a procedure that explicitly or
  1231.      implicitly declares that argument `REAL(KIND=1)'.
  1232.  
  1233.      For example, a procedure with no `DOUBLE PRECISION' or `IMPLICIT
  1234.      DOUBLE PRECISION' statement specifying the dummy argument
  1235.      corresponding to an actual argument of `REAL(Z)', where `Z' is
  1236.      declared `DOUBLE COMPLEX', strongly suggests that the programmer
  1237.      expected `REAL(Z)' to return `REAL(KIND=1)' instead of
  1238.      `REAL(KIND=2)'.
  1239.  
  1240.    * It is used in a context that would otherwise not include any
  1241.      `REAL(KIND=2)' but where treating the INTRINSIC invocation as
  1242.      `REAL(KIND=2)' would result in unnecessary promotions and
  1243.      (typically) more expensive operations on the wider type.
  1244.  
  1245.      For example:
  1246.  
  1247.           DOUBLE COMPLEX Z
  1248.           ...
  1249.           R(1) = T * REAL(Z)
  1250.  
  1251.      The above example suggests the programmer expected the real part
  1252.      of `Z' to be converted to `REAL(KIND=1)' before being multiplied
  1253.      by `T' (presumed, along with `R' above, to be type `REAL(KIND=1)').
  1254.  
  1255.      Otherwise, the conversion would have to be delayed until after the
  1256.      multiplication, requiring not only an extra conversion (of `T' to
  1257.      `REAL(KIND=2)'), but a (typically) more expensive multiplication
  1258.      (a double-precision multiplication instead of a single-precision
  1259.      one).
  1260.  
  1261.    The result of INTRINSIC is expected (by the original programmer) to
  1262. be `REAL(KIND=2)' (the Fortran 90 interpretation) if:
  1263.  
  1264.    * It is passed as an argument to a procedure that explicitly or
  1265.      implicitly declares that argument `REAL(KIND=2)'.
  1266.  
  1267.      For example, a procedure specifying a `DOUBLE PRECISION' dummy
  1268.      argument corresponding to an actual argument of `REAL(Z)', where
  1269.      `Z' is declared `DOUBLE COMPLEX', strongly suggests that the
  1270.      programmer expected `REAL(Z)' to return `REAL(KIND=2)' instead of
  1271.      `REAL(KIND=1)'.
  1272.  
  1273.    * It is used in an expression context that includes other
  1274.      `REAL(KIND=2)' operands, or is assigned to a `REAL(KIND=2)'
  1275.      variable or array element.
  1276.  
  1277.      For example:
  1278.  
  1279.           DOUBLE COMPLEX Z
  1280.           DOUBLE PRECISION R, T
  1281.           ...
  1282.           R(1) = T * REAL(Z)
  1283.  
  1284.      The above example suggests the programmer expected the real part
  1285.      of `Z' to *not* be converted to `REAL(KIND=1)' by the `REAL()'
  1286.      intrinsic.
  1287.  
  1288.      Otherwise, the conversion would have to be immediately followed by
  1289.      a conversion back to `REAL(KIND=2)', losing the original, full
  1290.      precision of the real part of `Z', before being multiplied by `T'.
  1291.  
  1292.    Once you have determined whether a particular invocation of INTRINSIC
  1293. expects the Fortran 90 interpretation, you can:
  1294.  
  1295.    * Change it to `DBLE(EXPR)' (if INTRINSIC is `REAL') or
  1296.      `DIMAG(EXPR)' (if INTRINSIC is `AIMAG') if it expected the Fortran
  1297.      90 interpretation.
  1298.  
  1299.      This assumes EXPR is `COMPLEX(KIND=2)'--if it is some other type,
  1300.      such as `COMPLEX*32', you should use the appropriate intrinsic,
  1301.      such as the one to convert to `REAL*16' (perhaps `DBLEQ()' in
  1302.      place of `DBLE()', and `QIMAG()' in place of `DIMAG()').
  1303.  
  1304.    * Change it to `REAL(INTRINSIC(EXPR))', otherwise.  This converts to
  1305.      `REAL(KIND=1)' in all working Fortran compilers.
  1306.  
  1307.    If you don't want to change the code, and you are certain that all
  1308. ambiguous invocations of INTRINSIC in the source file have the same
  1309. expectation regarding interpretation, you can:
  1310.  
  1311.    * Compile with the `g77' option `-ff90', to enable the Fortran 90
  1312.      interpretation.
  1313.  
  1314.    * Compile with the `g77' options `-fno-f90 -fugly-complex', to
  1315.      enable the non-Fortran-90 interpretations.
  1316.  
  1317.    *Note REAL() and AIMAG() of Complex::, for more information on this
  1318. issue.
  1319.  
  1320.    Note: If the above suggestions don't produce enough evidence as to
  1321. whether a particular program expects the Fortran 90 interpretation of
  1322. this ambiguous invocation of INTRINSIC, there is one more thing you can
  1323. try.
  1324.  
  1325.    If you have access to most or all the compilers used on the program
  1326. to create successfully tested and deployed executables, read the
  1327. documentation for, and *also* test out, each compiler to determine how
  1328. it treats the INTRINSIC intrinsic in this case.  (If all the compilers
  1329. don't agree on an interpretation, there might be lurking bugs in the
  1330. deployed versions of the program.)
  1331.  
  1332.    The following sample program might help:
  1333.  
  1334.            PROGRAM JCB003
  1335.      C
  1336.      C Written by James Craig Burley 1997-02-23.
  1337.      C
  1338.      C Determine how compilers handle non-standard REAL
  1339.      C and AIMAG on DOUBLE COMPLEX operands.
  1340.      C
  1341.            DOUBLE COMPLEX Z
  1342.            REAL R
  1343.            Z = (3.3D0, 4.4D0)
  1344.            R = Z
  1345.            CALL DUMDUM(Z, R)
  1346.            R = REAL(Z) - R
  1347.            IF (R .NE. 0.) PRINT *, 'REAL() is Fortran 90'
  1348.            IF (R .EQ. 0.) PRINT *, 'REAL() is not Fortran 90'
  1349.            R = 4.4D0
  1350.            CALL DUMDUM(Z, R)
  1351.            R = AIMAG(Z) - R
  1352.            IF (R .NE. 0.) PRINT *, 'AIMAG() is Fortran 90'
  1353.            IF (R .EQ. 0.) PRINT *, 'AIMAG() is not Fortran 90'
  1354.            END
  1355.      C
  1356.      C Just to make sure compiler doesn't use naive flow
  1357.      C analysis to optimize away careful work above,
  1358.      C which might invalidate results....
  1359.      C
  1360.            SUBROUTINE DUMDUM(Z, R)
  1361.            DOUBLE COMPLEX Z
  1362.            REAL R
  1363.            END
  1364.  
  1365.    If the above program prints contradictory results on a particular
  1366. compiler, run away!
  1367.  
  1368.